{% extends "base.html" %}
{% block content %}
<h1>Custom Context: display time </h1>
<p>
  First view to use a real variable, <code>now</code>. It is basically
  the present time: <strong>{{ now }}</strong>. Update time by <a
  href="">refreshing</a>. The sole purpose is to demonstrate the
  function <code>build_params</code> that you may use to populate your
  context dynamically.</p>
<p>
  <strong><code>build_params</code></strong> will naturally be given
  all args/kwargs you put in your URLs and will also have access to
  any static variable defined as seen previously. In this example, url
  only defines one named param (name) <code>build_params</code> is
  defined consequently.
</p>
<div class="highlight"><pre>
<span class="kn">from</span> <span class="nn">obviews.views</span> <span class="kn">import</span> <span class="n">View</span><span class="p">,</span> <span class="n">FormView</span>

<span class="c"># adding variables in context at call time</span>
<span class="k">class</span> <span class="nc">ContextView</span><span class="p">(</span><span class="n">View</span><span class="p">):</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s">&quot;now.html&quot;</span>

    <span class="k">def</span> <span class="nf">build_params</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s">&quot;guest&quot;</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">{</span><span class="s">&quot;name&quot;</span><span class="p">:</span> <span class="n">name</span><span class="p">,</span>
                <span class="s">&quot;now&quot;</span><span class="p">:</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">()}</span>
    
<span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">,</span>
    <span class="p">(</span><span class="s">&#39;^now/$&#39;</span><span class="p">,</span> <span class="n">ContextView</span><span class="p">()),</span>
    <span class="p">(</span><span class="s">&#39;^now/(?P&lt;name&gt;\w+)/$&#39;</span><span class="p">,</span> <span class="n">ContextView</span><span class="p">()),</span>
<span class="p">)</span>

</pre></div>
<p>
  Note that request is also passed to build_params, you consequently
  have the same functionalities as in any view defined as a function,
  and more...
</p>
<a name="inheriting" />
<h1>Inheriting </h1>
<p>
  This same template is also used by a view inheriting from the <a
  href="/now/{{ name|lower }}/">one above</a></p>
<p>
  The only difference is to display the variable <code>'name'</code> <a
  href="/now/{{ name|lower }}/cap/">uppercase</a>. This is achieve
  inside the <code>'build_params'</code> method wich is the place where you will
  put your standard view-code</p>
<div class="highlight"><pre>
<span class="c"># inheriting ! Even more DRY...</span>
<span class="c"># adding more vars in context according given arguments passed through URL</span>
<span class="k">class</span> <span class="nc">ComplexView</span><span class="p">(</span><span class="n">ContextView</span><span class="p">):</span>

    <span class="k">def</span> <span class="nf">build_params</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">cap</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="n">params</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">ComplexView</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">build_params</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="n">name</span><span class="p">)</span>
        <span class="n">params</span><span class="p">[</span><span class="s">&quot;name&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">cap</span><span class="o">==</span><span class="s">&quot;cap&quot;</span> <span class="ow">and</span> <span class="n">name</span><span class="o">.</span><span class="n">upper</span><span class="p">()</span> <span class="ow">or</span> <span class="n">name</span>
        <span class="k">return</span> <span class="n">params</span>
    
<span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">,</span>
    <span class="p">(</span><span class="s">&#39;^now/(?P&lt;name&gt;\w+)/(?P&lt;cap&gt;cap|nocap)/$&#39;</span><span class="p">,</span> <span class="n">ComplexView</span><span class="p">(</span><span class="n">cap</span><span class="o">=</span><span class="bp">True</span><span class="p">)),</span>
<span class="p">)</span>

</pre></div>
<p>
  In a similar way, you may use a class var to change the <a
  href="/forbidden/">type of response returned by the
  view</a>... which is shown <strong>next step</strong>.
</p>
{% endblock %}
